package com.publiccms.admin.views.controller.system;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.publiccms.common.tools.UserUtils;
import com.publiccms.entities.log.LogOperate;
import com.publiccms.entities.system.SystemDept;
import com.publiccms.logic.service.log.LogOperateService;
import com.publiccms.logic.service.system.SystemDeptService;
import com.sanluan.common.base.BaseController;
import com.sanluan.common.tools.RequestUtils;

@Controller
@RequestMapping("systemDept")
public class SystemDeptController extends BaseController {
	@Autowired
	private SystemDeptService service;
	@Autowired
	private LogOperateService logOperateService;

	@RequestMapping("save")
	public String save(SystemDept entity, HttpServletRequest request) {
		if (notEmpty(entity.getId())) {
			entity = service.update(entity.getId(), entity, new String[] { "id", "childIds" });
			if (notEmpty(entity)) {
				logOperateService.save(new LogOperate(UserUtils.getAdminFromSession(request).getId(), "update.dept", RequestUtils
						.getIp(request), getDate(), entity.getId() + ":" + entity.getName()));
			}
		} else {
			entity = service.save(entity);
			if (notEmpty(entity)) {
				logOperateService.save(new LogOperate(UserUtils.getAdminFromSession(request).getId(), "save.dept", RequestUtils
						.getIp(request), getDate(), entity.getId() + ":" + entity.getName()));
			}
		}
		return "common/ajaxDone";
	}

	@RequestMapping("delete")
	public String delete(Integer id, HttpServletRequest request) {
		SystemDept entity = service.delete(id);
		if (notEmpty(entity)) {
			logOperateService.save(new LogOperate(UserUtils.getAdminFromSession(request).getId(), "delete.dept", RequestUtils
					.getIp(request), getDate(), id + ":" + entity.getName()));
		}
		return "common/ajaxDone";
	}
}